﻿using System;
using System.Collections;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.ServiceModel.Web;
using System.Collections.Generic;
using System.Configuration;
using EsaProjectRest.Service;
using EsaProjectRest.Service.Google;
using EsaProjectRest.Facebook;
using EsaProjectRest.Twitter;
using System.Threading;

namespace EsaProjectRest
{
    public class ServiceRest : IServiceRest
    {
        //SQL connection with DB "name:EsaDB source:apat"
        public static ServiceRest serv = new ServiceRest();
        public static string set_conn = serv.setting_connection();
        public SqlConnection conn = new SqlConnection(set_conn);

        public string setting_connection()
        {
            System.Configuration.Configuration rootWebConfig =
            System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Web.config");
            System.Configuration.ConnectionStringSettings connString;
            if (0 < rootWebConfig.ConnectionStrings.ConnectionStrings.Count)
            {
                connString = rootWebConfig.ConnectionStrings.ConnectionStrings["LocalSqlServer"];
                if (null != connString)
                    return connString.ConnectionString;
                else
                    return "error";
            }
            else return "error";
        }
        //public SqlConnection conn = new SqlConnection("Data Source=apat;Initial Catalog=EsaDB;Integrated Security=True;Pooling=False");
        
        //open connession to DB
        public void OpenConnection()
        {
            try
            {
                conn.Open();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }
        }

        //close connession to DB
        public void CloseConnection()
        {
            try
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }
        }

        //Update all posts in DB for each user in the system
        
        public Boolean UpdatePostsDB(string id)
        {            
            OpenConnection();
            try
            {
                //per renderlo automatico su tutti gli utenti togliere il where
                DataSet vds = new DataSet();
                string vsql = string.Format("SELECT COUNT(Id) AS Tot FROM  [User] WHERE (Id = '" + id + "')");
                SqlCommand vCom = new SqlCommand(vsql, conn);
                SqlDataAdapter vda = new SqlDataAdapter(vCom);
                vda.Fill(vds);
                string rows = vds.Tables[0].Rows[0]["Tot"].ToString();
                int tot_rows = Int32.Parse(rows);

                string[] id_user = new string[tot_rows];
                DataSet vds2 = new DataSet();
                string sql = string.Format("SELECT Id FROM [User] WHERE (Id = '" + id + "') ");
                SqlCommand vCom2 = new SqlCommand(sql, conn);
                SqlDataAdapter vda2 = new SqlDataAdapter(vCom2);
                vda2.Fill(vds2);
                for (int i = 0; i < tot_rows; i++)
                {
                    id_user[i] = vds2.Tables[0].Rows[i]["Id"].ToString();
                }

                try
                {
                    for (int i = 0; i < tot_rows; i++)
                    {
                        PostFB fb = new PostFB();
                        Boolean risFB = fb.MyPostsFB(id_user[i]);
                        Boolean risFB2 = fb.MyTagsFB(id_user[i]);
                        Boolean risFB3 = fb.UpdateMyPostsFB(id_user[i]);

                        PostTW tw = new PostTW();
                        Boolean risTW = tw.MyPostsTW(id_user[i]);
                        Boolean risTW2 = tw.MyTagsTW(id_user[i]);
                        Boolean risTW3 = tw.UpdateMyPostTW(id_user[i]);

                        PostGO go = new PostGO();
                        Boolean risGO = go.MyPostsGO(id_user[i]);
                        Boolean risGO2 = go.UpdateMyPostGo(id_user[i]);
                    }

                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex.Message);
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
            //return true;
        }

        //register to the system
        public Boolean RegisterCompany(string email, string password, string logo, string name, string description)
        {
            User user = new User();
            bool ris = user.RegisterCompany(email, password, logo, name, description);
            return ris;
        }

        //login to the system
        public string LoginCompany(string email, string password)
        {
            User user = new User();
            string id = user.LoginCompany(email, password);
            return id;
        }

        //write token social network application in DB
        public Boolean InsertToken(string id, string access_token, string service, string url)
        {
            Services services = new Services();
            Boolean ris = services.InsertToken(id, access_token, service, url);
            return ris;

        }

        public string[] GetPublicPage(string id, string service)
        {
            Services services = new Services();
            string[] ris = services.GetPublicPage(id, service);
            return ris;
        }

        public Boolean InsertTokenPage(string id, string access_token, string service)
        {
            Services services = new Services();
            Boolean ris = services.InsertTokenPage(id, access_token, service);
            return ris;

        }

        //retrive information for one user by id
        public string[] GetDataUser(string id)
        {
            User user = new User();
            string[] ris = user.GetDataUser(id);
            return ris;
        }

        //retrive the SN services connect 
        public string[] GetServiceOn(string id)
        {
            User user = new User();
            string[] ris = user.GetServiceOn(id);
            return ris;
        }

        //retrieve avatar for one user by id
        public string[] GetAvatarUser(string id)
        {
            User user = new User();
            string[] ris = user.GetAvatarUser(id);
            return ris;
        }

        //update logo 
        public Boolean UpdateLogo(string id, string img)
        {
            User user = new User();
            bool ris = user.UpdateLogo(id, img);
            return ris;
        }

        //delete token for one associated service
        public Boolean DeleteToken(string id, string service)
        {
            Services services = new Services();
            bool ris = services.DeleteToken(id, service);
            return ris;
        }

        //retrive posts from social network serices
        public string[] GetMyPosts(string id, string where, string start_offset, string end_offset)
        {
            Services services = new Services();
            string[] ris = services.GetMyPosts(id, where, start_offset, end_offset);
            return ris;
        }

        //retrive tags from social network serices
        public string[] GetMyTags(string id, string where, string start_offset, string end_offset)
        {
            Services services = new Services();
            string[] ris = services.GetMyTags(id, where, start_offset, end_offset);
            return ris;
        }

        public Boolean UpdateMyPostsFB(string id)
        {
            PostFB fb = new PostFB();
            Boolean ris = fb.UpdateMyPostsFB(id);
            return ris;
        }
        //retrive posts only for facebook service
        public Boolean MyPostsFB(string id)
        {
            PostFB fb = new PostFB();
            Boolean ris = fb.MyPostsFB(id);
            return ris;
        }

        public Boolean UpdateMyPostTW(string id)
        {
            PostTW tw = new PostTW();
            Boolean ris = tw.UpdateMyPostTW(id);
            return ris;
        }
        //retrive posts only for twitter service
        public Boolean MyPostsTW(string id)
        {
            PostTW tw = new PostTW();
            Boolean ris = tw.MyPostsTW(id);
            return ris;
        }

        public Boolean UpdateMyPostGo(string id)
        {
            PostGO go = new PostGO();
            Boolean ris = go.UpdateMyPostGo(id);
            return ris;
        }
        //retrive posts only for  service
        public Boolean MyPostsGO(string id)
        {
            PostGO go = new PostGO();
            Boolean ris = go.MyPostsGO(id);
            return ris;
        }

        public Boolean MyTagsFB(string id)
        {
            PostFB fb = new PostFB();
            Boolean ris = fb.MyTagsFB(id);
            return ris;
        }

        public Boolean MyTagsTW(string id)
        {
            PostTW tw = new PostTW();
            Boolean ris = tw.MyTagsTW(id);
            return ris;
        }

        public Boolean CrossPostingTW(string id, string message)
        {
            PostTW tw = new PostTW();
            Boolean ris = tw.CrossPostingTW(id, message);
            return ris;
        }

        public Boolean CrossPostingFB(string id, string message)
        {
            PostFB fb = new PostFB();
            Boolean ris = fb.CrossPostingFB(id, message);
            return ris;
        }

        public Boolean DeletePost(string id, string service)
        {
            Services services = new Services();
            Boolean ris = services.DeletePost(id, service);
            return ris;
        }

        public Boolean N_Liker(string id, string service)
        {
            Services services = new Services();
            Boolean ris = services.N_Liker(id, service);
            return ris;

        }


        //ESA INSIGHT
        public float ApplauseRate(string id)
        {
            EsaInsight insight = new EsaInsight();
            float ris = insight.ApplauseRate(id);
            return ris;
        }

        public float ApplauseRateFB(string id)
        {
            EsaInsight insight = new EsaInsight();
            float ris = insight.ApplauseRateFB(id);
            return ris;
        }

        public float ApplauseRateGO(string id)
        {
            EsaInsight insight = new EsaInsight();
            float ris = insight.ApplauseRateGO(id);
            return ris;
        }

        public float ConversationRate(string id)
        {
            EsaInsight insight = new EsaInsight();
            float ris = insight.ConversationRate(id);
            return ris;
        }

        public float ConversationRateFB(string id)
        {
            EsaInsight insight = new EsaInsight();
            float ris = insight.ConversationRateFB(id);
            return ris;
        }

        public float ConversationRateGO(string id)
        {
            EsaInsight insight = new EsaInsight();
            float ris = insight.ConversationRateGO(id);
            return ris;
        }

        public float AmplificationRate(string id)
        {
            EsaInsight insight = new EsaInsight();
            float ris = insight.AmplificationRate(id);
            return ris;
        }

        public float AmplificationRateTW(string id)
        {
            EsaInsight insight = new EsaInsight();
            float ris = insight.AmplificationRateTW(id);
            return ris;
        }

        public float AmplificationRateGO(string id)
        {
            EsaInsight insight = new EsaInsight();
            float ris = insight.AmplificationRateGO(id);
            return ris;
        }

        public string[] InteractionRate(string id)
        {
            EsaInsight insight = new EsaInsight();
            string[] ris = insight.InteractionRate(id);
            return ris;
        }

        public string[] Engagement_28(string id)
        {
            EsaInsight insight = new EsaInsight();
            string[] ris = insight.Engagement_28(id);
            return ris;
        }

        public string[] Passion(string id)
        {
            EsaInsight insight = new EsaInsight();
            string[] ris = insight.Passion(id);
            return ris;
        }
    }
}
